package com.shuiyun.jtt.utils;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: 107
 * @date: 2018/11/22 16:19
 * @description:
 * @Review:
 */
@Slf4j
public class DateUtils {

    public static long PERDAY_MILLS = 86400000;
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    /**
     * 十分钟的毫秒数
     */
    private static final int TEN_MINUTE = 60 * 10 * 1000;

    public static Integer parseDate(Integer time) {
        Date date = new Date(time * 1000L);
        String yyyyMMdd = DateUtils.format(date, "yyyyMMdd");
        return Integer.parseInt(yyyyMMdd);
    }

    public static String formatDate(Integer time) {
        Date date = new Date(time * 1000L);
        String yyyyMMdd = DateUtils.format(date, "yyMMddHHmmss");
        return yyyyMMdd;
    }

    public static Long getNowSecond(Integer time){
        Date beginDay = DateUtil.beginOfDay(new Date(time * 1000L));
        return (time * 1000L - beginDay.getTime()) / 1000L;
    }

    public static String getYYYYMMDDHHMMSS(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String format(Date date,String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 将当前秒数转为日期字符串
     *
     * @param mill 当前秒
     * @return
     */
    public static String getSecondToDateString(Long mill) {
        Date date = new Date(mill * 1000);
        return getStardardTime(date);
    }

    /**
     * 将当前毫秒数转为日期字符串
     *
     * @param mill 当前秒
     * @return
     */
    public static String getMillToDateString(Long mill) {
        Date date = new Date(mill);
        return getStardardTime(date);
    }

    /**
     * 获取当天时间yyyyMMdd格式的int数字,20180911
     *
     * @param date
     * @return
     */
    public static Integer get00CLockToInteger(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateString = sdf.format(date);
        return Integer.parseInt(dateString);
    }

    /**
     * 获取十分钟前的毫秒数
     *
     * @param date
     * @return
     */
    public static Long getTenMinuteAgoMill(Date date) {
        return date.getTime() - TEN_MINUTE;
    }

    public static Long getNowMill(Date date) {
        return date.getTime() / 1000;
    }

    public static String yyyyMMddDays(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String getStardardTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 获取第三天凌晨0点0分0秒的时间
     *
     * @return
     */
    public static Date getTheDayAfterTomorrow() {
        Date tomorrow = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String today = format.format(new Date());
        try {
            tomorrow = new Date(format.parse(today).getTime() + PERDAY_MILLS * 2);
        } catch (ParseException e) {
            log.warn("日期转换失败");
        }

        return tomorrow;
    }


    /**
     * 获取第二天凌晨0点0分0秒的时间
     *
     * @return
     */
    public static Date getTomorrow() {
        Date tomorrow = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String today = format.format(new Date());
        try {
            tomorrow = new Date(format.parse(today).getTime() + PERDAY_MILLS);
        } catch (ParseException e) {
            log.warn("日期转换失败");
        }

        return tomorrow;
    }

    /**
     * 获取今天凌晨0点0分0秒的时间
     *
     * @return
     */
    public static Long getTodayZeroMills() {
        Long todayMills = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String today = format.format(new Date());
        try {
            todayMills = format.parse(today).getTime();
        } catch (ParseException e) {
            log.warn("日期转换失败");
        }
        return todayMills;
    }

    public static long getYesterdayStart() {
        return getTodayZeroMills() - PERDAY_MILLS;
    }

    public static Integer getYesterdayDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String yesterdayStr = format.format(new Date().getTime() - PERDAY_MILLS);
        return Integer.parseInt(yesterdayStr);
    }

    public static long getYesterdayEnd() {
        //当前天0时0分0秒
        return getTodayZeroMills() - 1;
    }

    public static long getLastWeekEnd() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        long lastWeekEndZeroMills = 0L;
        try {
            lastWeekEndZeroMills = format.parse(getLastWeekEndDate().toString()).getTime();
        } catch (ParseException e) {
            throw new RuntimeException("获取上周结束时间失败");
        }
        return lastWeekEndZeroMills + PERDAY_MILLS - 1;
    }

    public static Integer getLastWeekEndDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.add(Calendar.DATE, -1);
        return Integer.parseInt(format.format(calendar.getTimeInMillis()));
    }

    public static long getLastWeekStart() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            return format.parse(getLastWeekStartDate().toString()).getTime();
        } catch (ParseException e) {
            throw new RuntimeException("获取上周开始时间失败");
        }
    }

    public static Integer getLastWeekStartDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -7);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return Integer.parseInt(format.format(calendar.getTimeInMillis()));
    }

    public static long getLastMonthEnd() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        long lastMonthEndZeroMills = 0L;
        try {
            lastMonthEndZeroMills = format.parse(getLastMonthEndDate().toString()).getTime();
        } catch (ParseException e) {
            throw new RuntimeException("获取上周结束时间失败");
        }
        return lastMonthEndZeroMills + PERDAY_MILLS - 1;
    }

    public static Integer getLastMonthEndDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        return Integer.parseInt(format.format(calendar.getTimeInMillis()));
    }

    public static long getLastMonthStart() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            return format.parse(getLastMonthStartDate().toString()).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static Integer getLastMonthStartDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return Integer.parseInt(format.format(calendar.getTimeInMillis()));
    }

    /**
     * 获取开始日期（>=)
     *
     * @param
     * @param startTime 毫秒
     * @return
     */
    public static Integer getStartDate(Long startTime) {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        //   String today = format.format(startTime * MILLRATE);
        String today = format.format(startTime);
        Integer startDate = null;
        try {
            startDate = Integer.parseInt(today);
        } catch (NumberFormatException e) {
            log.warn("日期转换失败");
        }

        return startDate;
    }

    /**
     * 获取结束日期（<=）
     *
     * @param endTime 毫秒
     * @return
     */
    public static Integer getEndDate(Long endTime) {
        //获取的是第二天凌晨0点0分0秒的时间，xml中endDate是>=,所以这里减10分钟，让结束日期为前一天
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Integer tenmin = 60 * 1000 * 10;
        // String txtday = format.format(endTime * MILLRATE - tenmin);
        String txtday = format.format(endTime - tenmin);
        Integer endDate = null;
        try {
            endDate = Integer.parseInt(txtday);
        } catch (NumberFormatException e) {
            log.warn("日期转换失败");
        }
        return endDate;
    }

    /**
     * @desc: 10进制串转为BCD码
     * @param: 10进制串
     * @return: BCD码
     */
    public static byte[] str2Bcd(String asc) {
        int len = asc.length();
        int mod = len % 2;
        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }
        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }
        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;
        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }
            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }
            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }

}
